﻿using Microsoft.Win32;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Tools.Core;
using Tools.Core.Dtos;
using Tools.Core.Extensions;
using Tools.Core.Mvvm;

namespace Tools.Modules.Navigations.ViewModels
{
    public class ImageOCRViewModel : BindableBase,INavigationAware
    {
        private string _result = string.Empty;
        public string Result
        {
            get => _result;
            set=>SetProperty(ref _result, value);
        }
        private ObservableCollection<ImageOCRPlanDto> _plans=new ObservableCollection<ImageOCRPlanDto>();
        public ObservableCollection<ImageOCRPlanDto> Plans
        {
            get => _plans;
            set => SetProperty(ref _plans, value);
        }
        public DelegateCommand ChooseImagesCommand { get; private set; }
        public DelegateCommand<ImageOCRPlanDto> ShowResultCommand { get; private set; }
        public DelegateCommand SaveResultCommand { get; private set; }
        public ImageOCRViewModel()
        {
            ChooseImagesCommand = new DelegateCommand(() =>
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "图片|*.png;*.jpg;*.jpeg;*.bmp;*.gif";
                ofd.Multiselect = true;
                if (ofd.ShowDialog() == true)
                {
                    var addPlans=ofd.FileNames.Select(name=>new ImageOCRPlanDto
                    {
                        Name = Path.GetFileNameWithoutExtension(name),
                        CreateTime= DateTime.Now,
                        Completed=false,
                        FromPath=name
                    }).AsObservableCollection();
                    Plans = Plans.Concat(addPlans).AsObservableCollection();
                    TasksQueueHelper.GetQueue("ImageOCR").Enqueue(() =>
                    {
                        Softwares.OCRImages(addPlans);
                        return Task.CompletedTask;
                    });
                }
            });
            ShowResultCommand = new DelegateCommand<ImageOCRPlanDto>(plan =>
            {
                Result = plan.Result;
            });
            SaveResultCommand = new DelegateCommand(() =>
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "文本文件|*.txt";
                sfd.DefaultExt = ".txt";
                if (sfd.ShowDialog() == true)
                {
                    File.WriteAllText(sfd.FileName, Result);
                }
            });
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
        }
    }
}
